package com.ijunfu.study.jdk.jdk8.date;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Title : 日期时间解析和格式化示例
 * @Remarks:
 * @Author : Weizhiguo
 * @Version: 1.0.0
 * @Date : 2022-04-30
 */
public class DateTimeFormatterDemo {

    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

        // 1. 将日期时间转为字符串
        System.out.println(now.format(dateTimeFormatter));

        // 2. 自定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(now.format(formatter));

        // 3. 解析字符串类型的日期时间
        LocalDateTime dateTime = LocalDateTime.parse("2022-04-30 17:53:34", formatter);
        System.out.println(dateTime);
    }
}
